package com.four.reimbursemanage.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.four.reimbursemanage.entity.Payapply;
import com.four.reimbursemanage.entity.Workapply;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

@Mapper
public interface WorkapplyMapper extends BaseMapper<Workapply> {

    /**
     * 查询加班补助申请列表
     *
     */
    @Select("<script>" +
            "SELECT * FROM workapply" +
            "<where>" +
            "    <if test=\"month != null\">" +
            "      and month = #{month}" +
            "   </if>" +
            "    <if test=\"name != null\">" +
            " <bind name=\"Key\" value=\"'%'+name+'%'\"/>" +
            "        name like #{Key} " +
            "    </if>" +
            "    <if test=\"state != null\">" +
            "      and state = #{state}" +
            "   </if>" +
            "</where>" +
            "limit #{start}, #{size}" +
            "</script>")
    public List<Workapply> selectWorkapplyList(int start, int size,
                                             String name, Date month, Integer state);


    /**
     * 查询本人加班补助申请列表
     *
     */
    @Select("<script>" +
            "SELECT * FROM workapply" +
            "<where>" +
            "    <if test=\"month != null\">" +
            "      and month = #{month}" +
            "   </if>" +
            "    <if test=\"name != null\">" +
            " <bind name=\"Key\" value=\"'%'+name+'%'\"/>" +
            "        name like #{Key} " +
            "    </if>" +
            "    <if test=\"state != null\">" +
            "      and state = #{state}" +
            "   </if>" +
            "    <if test=\"number != null\">" +
            "      and number = #{number}" +
            "   </if>" +
            "</where>" +
            "limit #{start}, #{size}" +
            "</script>")
    public List<Workapply> selectUser(int start, int size,
                                               String name, Date month, Integer state, String number);

    /*分页*/
    @Select("<script>" +
            "SELECT count(*) FROM workapply" +
            "<where>" +
            "    <if test=\"month != null\">" +
            "      and month = #{month}" +
            "   </if>" +
            "    <if test=\"name != null\">" +
            " <bind name=\"Key\" value=\"'%'+name+'%'\"/>" +
            "        name like #{Key} " +
            "    </if>" +
            "    <if test=\"month != null\">" +
            "      and month = #{month}" +
            "   </if>" +
            "    <if test=\"state != null\">" +
            "      and state = #{state}" +
            "   </if>" +
            "</where>" +
            "</script>")
    public int countWorkapply( String name,Date month, Integer state);

    /*本人分页*/
    @Select("<script>" +
            "SELECT count(*) FROM workapply" +
            "<where>" +
            "    <if test=\"month != null\">" +
            "      and month = #{month}" +
            "   </if>" +
            "    <if test=\"name != null\">" +
            " <bind name=\"Key\" value=\"'%'+name+'%'\"/>" +
            "        name like #{Key} " +
            "    </if>" +
            "    <if test=\"month != null\">" +
            "      and month = #{month}" +
            "   </if>" +
            "    <if test=\"state != null\">" +
            "      and state = #{state}" +
            "   </if>" +
            "    <if test=\"number != null\">" +
            "      and number = #{number}" +
            "   </if>" +
            "</where>" +
            "</script>")
    public int countWorkapplyUser( String name,Date month, Integer state, String number);

    /**
     * 新增加班补助申请
     */
    @Insert("insert into workapply (number,name,sumhour,worktype,worksum,external_num,worknum,workout_stady,holiday,other,advice,month,help,state)" +
            "values (#{number},#{name},#{sumhour},#{worktype},#{worksum},#{external_num},#{worknum},#{workout_stady},#{holiday},#{other},#{advice},#{month},#{help},4)")
    public int insertWorkapply(Workapply workapply);

    /**
     * 修改加班补助申请
     *

     */
    @Update("<script>" +
            "  update workapply" +
            "        <set>" +
            "            <if test=\"null != name\"> " +
            "                name = #{name}," +
            "            </if>" +
            "            <if test=\"null != sumhour\"> " +
            "                sumhour = #{sumhour}," +
            "            </if>" +
            "            <if test=\"null != worktype\"> " +
            "                worktype = #{worktype}," +
            "            </if>" +
            "            <if test=\"null != worksum\"> " +
            "                worksum = #{worksum}," +
            "            </if>" +
            "            <if test=\"null != external_num\"> " +
            "                external_num = #{external_num}," +
            "            </if>" +
            "            <if test=\"null != worknum\"> " +
            "                worknum = #{worknum}," +
            "            </if>" +
            "            <if test=\"null != workout_stady\"> " +
            "                workout_stady = #{workout_stady}," +
            "            </if>" +
            "            <if test=\"null != holiday\"> " +
            "                holiday = #{holiday}," +
            "            </if>" +
            "            <if test=\"null != other\"> " +
            "                other = #{other}," +
            "            </if>" +
            "            <if test=\"null != advice\"> " +
            "                advice = #{advice}," +
            "            </if>" +
            "            <if test=\"null != month\"> " +
            "                month = #{month}," +
            "            </if>" +
            "            <if test=\"null != help\"> " +
            "                help = #{help}," +
            "            </if>" +
            "            <if test=\"null != state\"> " +
            "                state = #{state}," +
            "            </if>" +
            "        </set>" +
            "        where id = #{id}" +
            "</script>")
    public int updateWorkapply(Workapply workapply);


    /*修改审批状态*/
    @Update("<script>" +
            "update  workapply " +
            "        <set>" +
            "            <if test=\"null!= state\">" +
            "                state =#{state}," +
            "            </if>" +
            "        </set>" +
            "        where id =#{id}" +
            "</script>")
    public int updateStateById(Workapply workapply);
}
